package com.baylor.widget;

import com.baylor.ui.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HeaderBarView extends RelativeLayout {
	
	private int layoutID;
	
    private boolean mMerging = false;
    
    private ImageView mImgBack;
    private TextView mTxtPreTitle;
    private TextView mTxtTitle;
    private TextView mTxtRightTitle;
    
    private View mBack;
    private OnBackLintener backLintener;
    
    private String nomalColor = "#ffffff";
    private String pressColor = "#e1e1e1";
    
	public HeaderBarView(Context context) {
        this(context, null);
    }
	
	public interface OnBackLintener {
		public void onClick();
	}

    public HeaderBarView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.gdActionBarStyle);
    }
    
    public HeaderBarView(Context context, AttributeSet attrs, int defStyle) {
    	super(context, attrs);
    	
    	TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, defStyle, 0);
    	
        layoutID = R.layout.common_actionbar;
        
        mMerging = true;
        LayoutInflater.from(context).inflate(layoutID, this);
        mMerging = false;
        
        a.recycle();
    }
    
    @Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		if( !mMerging ) {
			mImgBack = (ImageView) findViewById(R.id.img_back);
			mTxtPreTitle = (TextView) findViewById(R.id.actionbar_pre_title);
			mTxtTitle = (TextView) findViewById(R.id.actionbar_title);
			mTxtRightTitle = (TextView) findViewById(R.id.actionbar_right_title);
			
			mBack = findViewById(R.id.lin_back);
			mBack.setOnTouchListener(new OnTouchListener() {

				@SuppressLint("ClickableViewAccessibility")
				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					
					if( arg1.getAction() == MotionEvent.ACTION_DOWN ) {
						mTxtPreTitle.setTextColor( Color.parseColor(pressColor) );
						mImgBack.setImageResource(R.drawable.actionbar_btn_back_press);
					}

					if( arg1.getAction() == MotionEvent.ACTION_UP ) {
						if( backLintener != null ) {
							backLintener.onClick();
						}

						mTxtPreTitle.setTextColor( Color.parseColor(nomalColor) );
						mImgBack.setImageResource(R.drawable.actionbar_btn_back_normal);
					}
					
					return true;
				}
			});
			
		}
	}

	public void setOnBackLintener(CharSequence title, OnBackLintener listener) {
		if( mBack != null ) {
        	mBack.setVisibility(View.VISIBLE);
        	backLintener = listener;
		}
        if (mTxtPreTitle != null) {
        	mTxtPreTitle.setText(title);
        }
    }
	
	public void setTitle(CharSequence title) {
        if (mTxtTitle != null) {
        	mTxtTitle.setText(title);
        }
    }
	
	public void setRightTitle(CharSequence title) {
		if(mTxtRightTitle != null) {
			mTxtRightTitle.setText(title);
		}
	}
	
}
